package com.huazi.www.doctor.doctorc.dao.impl;

import com.huazi.www.doctor.doctorc.dao.MedicineDao;
import com.huazi.www.doctor.doctorc.entity.DrugEntity;
import com.huazi.www.doctor.doctorc.entity.MedicineEntity;
import com.huazi.www.doctor.doctorc.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户药单查询
 */
public class MedicineDaoImpl extends BaseDao implements MedicineDao {
    /**
     * 按用户姓名查询总记录数
     *
     * @param userName 用户姓名
     * @return 返回int类型
     */
    @Override
    public int TotalPageCountSelect(int docid, String userName) {
        //按照用户名查询查询用户药单总记录数
        String sql = " SELECT COUNT(1) FROM doctor_information AS infron,doctor_medicine AS me,drug_remedy AS rem WHERE me.doctorid=infron.doctorId AND me.medicineid=rem.id and infron.doctorId=? ";
        ResultSet rs = null;
        int count = 0;
        if (userName != null && !userName.equals("")) {//判断用户是否为空值
            sql += " AND (me.username LIKE CONCAT('%',?,'%') OR me.userid LIKE CONCAT('%',?,'%'))";//按照用户姓名和用户id模糊查询
            rs = executeQuery(sql, docid, userName, userName);//传入executeQuery方法查询
        } else {
            rs = executeQuery(sql, docid);//传入executeQuery方法查询
        }

        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeShow(rs, null, null);
        }

        return count;
    }

    /**
     * 按用户姓名分页查询
     *
     * @param userName   用户姓名
     * @param start      开始下标
     * @param lineNumber 要查询的行数
     * @return 返回List<MedicineEntity>类型
     */
    @Override
    public List<MedicineEntity> MedicineRecordSelect(int docid, String userName, Integer start, Integer lineNumber) {
        //按用户姓名查询
        String sql = "SELECT me.id ,me.userid,me.username,rem.drugname,me.sum,me.time,infron.doctorname,me.type FROM doctor_information AS infron,doctor_medicine AS me,drug_remedy AS rem WHERE me.doctorid=infron.doctorId AND me.medicineid=rem.id and infron.doctorId=?  ";
        ResultSet rs = null;
        List<MedicineEntity> list = new ArrayList<>();//list集合
        if (userName != null && !userName.equals("")) {//判断用户名是否为空
            sql += " AND (me.username LIKE CONCAT('%',?,'%') OR me.userid LIKE CONCAT('%',?,'%')) ORDER BY me.`time` DESC  LIMIT ?,?";//根据用户id模糊查询倒叙
            rs = executeQuery(sql, docid, userName, userName, start, lineNumber);//传入executeQuery方法查询
        } else {
            sql += " ORDER BY me.`time` DESC  LIMIT ?,?";//倒叙 选中查询行数
            rs = executeQuery(sql, docid, start, lineNumber);//传入executeQuery方法查询
        }
        try {
            while (rs.next()) {
                MedicineEntity entity = new MedicineEntity();//new MedicineEntity 实体类
                entity.setId(rs.getInt(1));//赋值setId
                entity.setUserid(rs.getInt(2));//赋值setUserid
                entity.setUsername(rs.getString(3));//赋值setUsername
                entity.setMedicine(rs.getString(4));//赋值setMedicine
                entity.setSum(rs.getInt(5));//赋值setSum
                entity.setTime(rs.getString(6));//赋值setTime
                entity.setDoctorId(rs.getString(7));//赋值setDoctorId
                entity.setType(rs.getString(8));//赋值setType
                list.add(entity);//存入list集合
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 根据药品id删除
     *
     * @param id
     */
    @Override
    public void delete(int id) {
        String sql = "DELETE FROM doctor_medicine where id=?";//根据药品id删除
        executeUpdate(sql, id);//传入executeUpdate方法
    }

    /**
     * 更具药品id修改药单信息
     *
     * @param me
     */
    @Override
    public void update(MedicineEntity me) {

        String sql = "update doctor_medicine set userid=?,username=?,medicineid=?,sum=?,doctorid=?,type=? where id=?";//药品id修改药单信息
        executeUpdate(sql, me.getUserid(), me.getUsername(), me.getMedicineid(), me.getSum(), me.getDid(), me.getType(), me.getId());//传入executeUpdate方法


    }

    /**
     * 查询 药品id 药品名 药品数量
     *
     * @return
     */
    @Override
    public List<DrugEntity> list() {
        String sql = "select id,drugname,count from drug_remedy";//查询 药品id 药品名 药品数量
        ResultSet rs = executeQuery(sql);//传入executeQuery方法
        List<DrugEntity> list = new ArrayList<>();//list<DrugEntity>泛型集合

        try {
            while (rs.next()) {
                DrugEntity drug = new DrugEntity();//new DrugEntity的实体
                drug.setId(rs.getInt(1));//赋值setId
                drug.setDrugname(rs.getString(2));//赋值setDrugname
                drug.setCount(rs.getInt(3));//赋值setCount
                list.add(drug);//存入list集合
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 查询旧密码是否成功！
     *
     * @param doctorid  医生id
     * @param doctorpwd 医生密码
     * @return
     */
    @Override
    public int pwdSelectOK(String doctorid, String doctorpwd) {
        String sql = "SELECT COUNT(1) FROM doctor_information AS infor,doctor_login AS login WHERE infor.doctorId=login.doctorid AND login.doctorid=? AND login.doctorpwd=?";//根据用户id 和用户密码查询
        int count = 0;
        ResultSet rs = executeQuery(sql, doctorid, doctorpwd);//传入executeUpdate
        try {
            rs.next();
            count = rs.getInt(1);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return count;
    }

    /**
     * 根据卡号修改密码
     *
     * @param doctorpwdxin 新密码
     * @param doctorid     卡号
     * @return
     */
    @Override
    public int updatePwd(String doctorpwdxin, String doctorid) {
        String sql = "UPDATE doctor_login SET doctorpwd=? WHERE doctorid=?";//根据用户卡号修改密码
        int count = executeUpdate(sql, doctorpwdxin, doctorid);//传入executeUpdate方法
        return count;
    }
}
